package descentFly.app.dialogs
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_inputLayer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import descentFly.app.df_block;
	import descentFly.app.df_component;
	import descentFly.app.df_events;
	import descentFly.app.df_label;
	import descentFly.app.df_modalLayer;
	import descentFly.app.df_window;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class df_baseLoginWindow extends df_window {
		public static const SIZE: Point = new Point(300, 170);
		
		private var _descHeight		: int;
		private var _description	: df_label;
		private var _login			: uif_inputLayer;
		private var _password		: uif_inputLayer;
		
		public function df_baseLoginWindow(parent: df_modalLayer, descr: String='', styleClass: String='window') {
			if (descr) {
				var style : Object = parent.app.styleByObj(styleClass);
				_descHeight = uif_util.calcTextHeight(descr, SIZE.x - parent.app.getStyleSpace(style, true) * 2, style.description);
			}
			super(parent, SIZE.x, SIZE.y + _descHeight, styleClass, true, 2);
			title.text = locale.LOGIN;
			if (_descHeight) _description.text = descr;
		}
		
		override protected function createDialogComponents(a_layer:df_block):void {
			var inner: Rectangle = a_layer.getInner();
			var value: Object = getValues();
			inner.top += 5;
			if (_descHeight) {
				_description = new df_label(a_layer, inner.x, inner.y, inner.width, _descHeight, style.description);
				inner.top += _description.height;
			}
			inner.top += 5;
			_login = new uif_inputLayer(a_layer, inner.x, inner.y, inner.width, INPUTHEIGHT, INPUTLABELWIDTH, locale.LOGIN, value.login, 'inputLayer');
			inner.top += INPUTHEIGHT;
			_password = new uif_inputLayer(a_layer, inner.x, inner.y, inner.width, INPUTHEIGHT, INPUTLABELWIDTH, locale.PASSWORD, value.password, 'inputLayer');
			_password.input.textField.displayAsPassword = true;
			
			addEvent(KeyboardEvent.KEY_DOWN, doKeyDown);
		}
		
		protected function doKeyDown(e: KeyboardEvent): void {
			if (e.keyCode == 13) doLoginOk();
		}
		
		protected function getValues(): Object {
			return {
				login: '',
				password: ''
			}
		}
		
		override protected function createButtons(a_layer:df_block):void {
			addButton(locale.OK, doLoginOk);
			addButton(locale.CANCEL, doClose);
		}
		
		private function doLoginOk(e: Event=null): void {
			if (!_login.value) parentLayer.dispatchEvent(new ObjectEvent(df_events.ALERT, locale.LOGINNOCORRECT));
			else if (!_password.value) parentLayer.dispatchEvent(new ObjectEvent(df_events.ALERT, locale.LOGINNOCORRECT));
			else loginComplete(_login.value, MD5.encrypt(_password.value));
		}
		
		protected function loginComplete(a_login: String, a_passwordEncrypt: String): void {
			
		}
	}
}